<template>
  <transition name="fade">
    <div class="loading-overlay" v-show="visible" @click.stop>
      <div class="loading-spinner"></div>
      <div class="loading-text">{{ text }}</div>
      <button class="cancel-button" @click="$emit('cancel')">取消</button>
    </div>
  </transition>
</template>

<script>
export default {
  name: 'LoadingOverlay',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    text: {
      type: String,
      default: 'AI正在努力生成题目...'
    },
    timeout: {
      type: Number,
      default: 15000
    }
  },
  emits: ['timeout', 'cancel'],
  data() {
    return {
      timer: null
    };
  },
  watch: {
    visible(val) {
       console.log('LoadingOverlay visible changed to:', val);
      if (val) {
        clearTimeout(this.timer);
        this.timer = setTimeout(() => {
          this.$emit('timeout');
        }, this.timeout);
      } else {
        clearTimeout(this.timer);
      }
    }
  },
  beforeUnmount() {
    clearTimeout(this.timer);
  }
};
</script>

<style scoped>
.loading-overlay {
  position: fixed;
  z-index: 99999;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(255, 255, 255, 0.9);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  pointer-events: auto; /* 允许交互 */
}

.loading-spinner {
  width: 50px;
  height: 50px;
  border: 5px solid #f3f3f3;
  border-top: 5px solid #125aa2;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

.loading-text {
  margin-top: 15px;
  font-weight: bold;
  color: #125aa2;
}

.cancel-button {
  margin-top: 20px;
  padding: 8px 16px;
  background-color: #d33;
  color: white;
  border: none;
  border-radius: 4px;
  font-weight: bold;
  cursor: pointer;
}

.cancel-button:hover {
  background-color: #b22;
}

@keyframes spin {
  0% { transform: rotate(0); }
  100% { transform: rotate(360deg); }
}

.fade-enter-active, .fade-leave-active {
  transition: opacity 0.3s;
}
.fade-enter-from, .fade-leave-to {
  opacity: 0;
}
</style>
